<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  <head>
    <title>Creating and using objects</title>
    <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
    <link rel="stylesheet" type="text/css" href="kawa.css"/>
    <script src="kawa-ebook.js" type="text/javascript"/>
    <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
    <link rel="prev" href="tutorial-Sequences.xhtml" title="Lists and sequences"/>
    <link rel="next" href="tutorial-Types.xhtml" title="Types and declarations"/>
  </head>
  <body>
    <header/>
    <section class="sect1" title="Creating and using objects" epub:type="subchapter" id="Tutorial---Objects">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both">Creating and using objects</h2>
          </div>
        </div>
      </div>
      <p>An <em class="firstterm">object</em> is a value that has the following features:
</p>
      <div class="itemizedlist" epub:type="list">
        <ul class="itemizedlist" style="list-style-type: disc; ">
          <li class="listitem" epub:type="list-item">
            <p>class - each object is an instance of a specific class,
making it part of the class hierarchy, which is an important
aspect of the type system;
</p>
          </li>
          <li class="listitem" epub:type="list-item">
            <p>properties - various fields and methods, depending on the class;
</p>
          </li>
          <li class="listitem" epub:type="list-item">
            <p>identity - it is distinct from all other objects,
even if all the properties are the same.
</p>
          </li>
        </ul>
      </div>
      <p>We later discuss <a class="link" href="tutorial-Classes.xhtml" title="Classes">how to write a new class</a>.
Here we assume you’re using an existing class, which
could be written in Java or Scheme.
</p>
      <h3 id="idm139667880413376">Creating a new object</h3>
      <p>To create a new object of class <code class="literal">T</code> you call <code class="literal">T</code> as if it
were a function, passing it the various constructor arguments:
</p>
      <pre class="screen">(java.io.File "src" "build.xml")
</pre>
      <p>If there are keyword arguments they are used to initialize
the corresponding named properties:
</p>
      <pre class="screen">(! button1 (javax.swing.JButton text: "Do it!" tool-tip-text:  "do it"))
</pre>
      <p>This create a new <code class="literal">JButton</code> object (using <code class="literal">JButton</code>’s
default constructor), and sets the <code class="literal">text</code> and <code class="literal">tool-tip-text</code>
properties (by calling <code class="literal">JButton</code>’s <code class="literal">setText</code>
and <code class="literal">setToolTipText</code> methods).
If there are constructor arguments, they must come before the keywords.
</p>
      <p>For objects that have components or elements, you
can list these at the end.  For example:
</p>
      <pre class="screen">(java.util.ArrayList 11 22 33)
</pre>
      <p>This creates a fresh <code class="literal">java.util.ArrayList</code> (using the
default constructor), and then calls the <code class="literal">add</code> method 3 times.
</p>
      <p>If you prefer you can use the <code class="literal">make</code> procedure,
but that only handle simple constructor calls:
</p>
      <pre class="screen">(make java.io.File "src" "build.xml")
</pre>
      <p>See <a class="link" href="Allocating-objects.xhtml" title="Allocating objects">Allocating objects</a> for details.
</p>
      <h3 id="idm139667880402240">Calling instance methods</h3>
      <p>Given an object <em class="replaceable"><code>obj</code></em> of a class that has a method <em class="replaceable"><code>meth</code></em>,
you can call it with argumens <em class="replaceable"><code>v1</code></em> ... <em class="replaceable"><code>v2</code></em> using <a class="link" href="Colon-notation.xhtml" title="Property access using colon notation">Colon notation</a>:
</p>
      <pre class="screen">(<em class="replaceable"><code>obj</code></em>:<em class="replaceable"><code>meth</code></em> <em class="replaceable"><code>v1</code></em> ... <em class="replaceable"><code>v2</code></em>)
</pre>
      <p>For example:
</p>
      <pre class="screen">(button1:paintImmediately 10 10 30 20)
</pre>
      <p>If you prefer, you can use the <code class="literal">invoke</code> procedure,
normally with a quoted method name:
</p>
      <pre class="screen">(invoke button1 'paintImmediately 10 10 30 20)
</pre>
      <p>You need to use <code class="literal">invoke</code> (rather than colon notation)
if <em class="replaceable"><code>obj</code></em> is a <code class="literal">Class</code> or a type expression, or its class
implements <code class="literal">gnu.mapping.HasNamedParts</code>.
</p>
      <p>See <a class="link" href="Method-operations.xhtml" title="Calling Java methods from Scheme">Method operations</a> for details.
</p>
      <h3 id="idm139667880391760">Accessing properties</h3>
      <p>If <em class="replaceable"><code>obj</code></em> has a field or property named <em class="replaceable"><code>fld</code></em> you can also use colon
notation:
</p>
      <pre class="screen"><em class="replaceable"><code>obj</code></em>:<em class="replaceable"><code>fld</code></em>
</pre>
      <p>You use the same syntax whether <em class="replaceable"><code>fld</code></em> is an actual field in the
object, or a <em class="firstterm">property</em> (in the Java Beans sense).  The
latter is implemented using a getter/setter pair:
Methods named <code class="literal">get<em class="replaceable"><code>F</code></em></code> and  <code class="literal">set<em class="replaceable"><code>F</code></em></code>, respectively.
For example:
</p>
      <pre class="screen">button1:tool-tip-text
</pre>
      <p>is equivalent to:
</p>
      <pre class="screen">(button1:getToolTipText)
</pre>
      <p>You can also change a field or property using colon notation:
</p>
      <pre class="screen">(set! <em class="replaceable"><code>obj</code></em>:<em class="replaceable"><code>fld</code></em> <em class="replaceable"><code>value</code></em>)
</pre>
      <p>For example:
</p>
      <pre class="screen">(set! button1:tool-tip-text "really do it!")
</pre>
      <p>This is equivalent to:
</p>
      <pre class="screen">(button1:setToolTipText "really do it!")
</pre>
      <p>Instead of colon notation, you can use the <code class="literal">field</code> procedure.
</p>
      <p>See <a class="link" href="Field-operations.xhtml" title="Accessing object fields">Field operations</a> for details.
</p>
      <h3 id="idm139667880375968">Static fields and methods</h3>
      <p>Kawa views static properties and methods as
properties and methods of the class itself.
To call a static method use the syntax:
</p>
      <pre class="screen">(<em class="replaceable"><code>clas</code></em>:<em class="replaceable"><code>meth</code></em> <em class="replaceable"><code>v1</code></em> ... <em class="replaceable"><code>vn</code></em>)
</pre>
      <p>For example:
</p>
      <pre class="screen">(java.math.BigDecimal:valueOf 12345 2) ⇒ 123.45
</pre>
      <p>To access a static field do <code class="literal"><em class="replaceable"><code>clas</code></em>:<em class="replaceable"><code>fld</code></em></code>.  For example:
</p>
      <pre class="screen">java.awt.Color:RED
</pre>
      <p>You can also use the <code class="literal">static-field</code> and <code class="literal">invoke-static</code> procedures.
</p>
    </section>
    <footer>
      <div class="navfooter">
        <p>
          Up: <a accesskey="u" href="tutorial-index.xhtml">Kawa Scheme Tutorial</a></p>
        <p>
        Previous: <a accesskey="p" href="tutorial-Sequences.xhtml">Lists and sequences</a></p>
        <p>
        Next: <a accesskey="n" href="tutorial-Types.xhtml">Types and declarations</a></p>
      </div>
    </footer>
  </body>
</html>
